﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.Web;
using CostaRicaPureTravelUtils.PRResourcesUtils;

namespace CRPTModularCorrousel.Menu
{
    public class MenuTemplate: ITemplate
    {

        private const string CLASS = "class";
        private const string STYLE = "style";
        private const string HREF = "href";
        private const string CURRENT_CLASS = "current";

        [Browsable(true)]
        [Category("Behavior")]
        public string ClassName { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
        public string Style { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
       
        public string Text { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
        public string Url { get; set; }

        [Browsable(true)]
        [Category("Behavior")]       
        public string LinkClass { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
        public string LinkStyle { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
        public string SubItemStyle { set; get; }

        public string PageName { set; get; }

        public void InstantiateIn(Control container)
        {            
            var li = new HtmlGenericControl("li");
            var link = new HtmlAnchor();
            if (!String.IsNullOrEmpty(ClassName)) 
            {
                li.Attributes.Add(CLASS, ClassName);
            }            
            if (!String.IsNullOrEmpty(Style))
            {
                li.Attributes.Add(STYLE, Style);
            }
            if (!String.IsNullOrEmpty(Url))
            {
                link.HRef = Url;                
            }
            if (!String.IsNullOrEmpty(LinkClass))
            {
                link.Attributes.Add(CLASS, LinkClass);
            }
            if (!String.IsNullOrEmpty(Text))
            {
                link.InnerText = Text;//PRResources.GetString(Text);
            }
            if (!String.IsNullOrEmpty(LinkStyle))
            {
                link.Attributes.Add(STYLE, LinkStyle);
            }
            if (SubItems != null)
            {
                bool isCurrent = InitiateSubItem(li);
                if (isCurrent)
                    li.Attributes.Add(CLASS, "current");
            }
            if (Url.Contains(PageName)) 
                li.Attributes.Add(CLASS, "current");
            li.Controls.Add(link);
            container.Controls.Add(li);
        }

        private bool InitiateSubItem(Control container) {
            bool isCurrent = false;
            var bulletList = new HtmlGenericControl("ul");
            foreach (var item in SubItems)
            {
                item.PageName = PageName;
                item.InstantiateIn(bulletList);
                if (item.Url.Contains(item.PageName))
                    isCurrent = true;
            }
            bulletList.Attributes.Add(STYLE, SubItemStyle);
            container.Controls.Add(bulletList);  
            return isCurrent;
        }

        [Browsable(true)]
        [Category("Behavior")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public MenuItemTemplate SubItems { get; set; }
    }
}
